<?php

class Catalogue_Model_Attribute_Object extends Catalogue_Model_Attribute {

    private static $_instances = 0;

    protected $_id = null;

    static function create($data, $field = null, $type = null, $options = null) {
        return new self($data, $field, $type, $options);
    }

    function __construct(Catalogue_Model_Object $object, $field = null, $type = null, $options = null) {

        if ($field instanceof Catalogue_Model_Attribute) {
            $attr = $field;
        } elseif (isInt($field)) {
            $attr = Catalogue_Model_Attribute::getInstance($field);
        } else {
            $attr = null;
        }
        
        if ($attr instanceof Catalogue_Model_Attribute) {

            $value = $object->getAttrValues($attr->getId());

            $attrId = $attr->getHtmlId();

            $this->_id = $attr->getId();

            $attr = $attr->toArray();

        } else {

            $attr = array(
                'type' => Catalogue_Model_Attribute::T_STRING,
                'multiple' => 0,
                'code' => $field,
                'name' => $field,
            );

            if (is_string($field)) {
                $value = $object[$field];
                $attrId = $field;
            } else {
                $attrId = self::HTML_PREFIX.self::$_instances;
            }

            $this->_id = self::$_instances;
        }

        parent::__construct($attr, $options);

		$this->_htmlId = $object->getHtmlId(null, $attrId);

        if (null !== $value) {
            $this->setDefaultValue($value);
        }

        if (null !== $type) {
            $this->setType($type);
        }

        self::$_instances++;

    }

    function getId() {
        return $this->_id;
    }

    function save() {
        throw new Workset_Model_Exception('You cannot save attribute of type '.get_class($this));
    }

    function delete() {
        throw new Workset_Model_Exception('You cannot delete attribute of type '.get_class($this));
    }

}


?>